package com.dimples.dd.system.controller;

import com.dimples.dd.common.constant.DDServerConstant;
import com.dimples.dd.common.pojo.PageResult;
import com.dimples.dd.common.result.CommonResult;
import com.dimples.dd.log.core.annotation.ApiLog;
import com.dimples.dd.log.core.enums.OperateTypeEnum;
import com.dimples.dd.system.model.form.OAuth2ClientForm;
import com.dimples.dd.system.model.query.OAuth2ClientPageQuery;
import com.dimples.dd.system.model.vo.OAuth2ClientPageVO;
import com.dimples.dd.system.service.Oauth2RegisteredClientService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


@Tag(name = "07.OAuth2 客户端")
@RestController
@RequestMapping("oauth2-client")
@Validated
@RequiredArgsConstructor
public class OAuth2ClientController {

    @Resource
    private Oauth2RegisteredClientService oauth2RegisteredClientService;

    @PostMapping
    @Operation(summary = "创建 OAuth2 客户端")
    @PreAuthorize("@ss.hasPerm('system:oauth2-client:create')")
    @ApiLog(operateType = OperateTypeEnum.CREATE, applicationName = DDServerConstant.DD_SERVER_SYSTEM, responseEnable = true)
    public CommonResult<String> createOAuth2Client(@Valid @RequestBody OAuth2ClientForm oAuth2ClientForm) {
        return CommonResult.success(this.oauth2RegisteredClientService.createOAuth2Client(oAuth2ClientForm));
    }

    @PutMapping("/{id}")
    @Operation(summary = "更新 OAuth2 客户端")
    @PreAuthorize("@ss.hasPerm('system:oauth2-client:update')")
    @ApiLog(operateType = OperateTypeEnum.UPDATE, applicationName = DDServerConstant.DD_SERVER_SYSTEM, responseEnable = true)
    public CommonResult<Boolean> updateOAuth2Client(
            @PathVariable("id") String id,
            @Valid @RequestBody OAuth2ClientForm updateForm
    ) {
        updateForm.setId(id);
        this.oauth2RegisteredClientService.updateOAuth2Client(updateForm);
        return CommonResult.success(true);
    }

    @DeleteMapping("/{ids}")
    @Operation(summary = "删除 OAuth2 客户端")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPerm('system:oauth2-client:delete')")
    @ApiLog(operateType = OperateTypeEnum.DELETE, applicationName = DDServerConstant.DD_SERVER_SYSTEM, responseEnable = true)
    public CommonResult<Boolean> deleteOAuth2Client(@PathVariable String ids) {
        this.oauth2RegisteredClientService.deleteOAuth2Client(ids);
        return CommonResult.success(true);
    }

    @GetMapping("/{id}/form")
    @Operation(summary = "获得 OAuth2 客户端")
    @PreAuthorize("@ss.hasPerm('system:oauth2-client:query')")
    @ApiLog(operateType = OperateTypeEnum.SELECT, applicationName = DDServerConstant.DD_SERVER_SYSTEM, responseEnable = true)
    public CommonResult<OAuth2ClientForm> selectOAuth2Client(@PathVariable String id) {
        return CommonResult.success(this.oauth2RegisteredClientService.selectOAuth2Client(id));
    }

    @GetMapping("/page")
    @Operation(summary = "获得 OAuth2 客户端分页")
    @PreAuthorize("@ss.hasPerm('system:oauth2-client:query')")
    @ApiLog(operateType = OperateTypeEnum.SELECT, applicationName = DDServerConstant.DD_SERVER_SYSTEM)
    public PageResult<OAuth2ClientPageVO> selectOAuth2ClientPage(@Valid OAuth2ClientPageQuery pageQuery) {
        return this.oauth2RegisteredClientService.selectOAuth2ClientPage(pageQuery);
    }

}
